package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderListDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.QueryString;
import com.itheima.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    private OrderListDao orderListDao;

    @Override
    public List<String> findSetmealToQueryString() {
        List<String> setmealNames = orderListDao.findSetmealToQueryString();
        return setmealNames;
    }

    @Override
    public PageResult findPage(Map map) {
        // 获取分页查询的条件
        Map queryString = (Map) map.get("queryString");

        // 获取分页参数
        Integer currentPage = (Integer) map.get("currentPage");
        Integer pageSize = (Integer) map.get("pageSize");


        JSONArray res = (JSONArray) queryString.get("orderTime");
        String startTime = null;
        String endTime = null;
        if (res != null) {
            String str = res.toString();

            String substring1 = str.substring(2, 12);
            String substring2 = str.substring(29, 39);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date data1 = null;
            Date data2 = null;
            try {
                data1 = simpleDateFormat.parse(substring1);
                data2 = simpleDateFormat.parse(substring2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            data1.setTime(data1.getTime() + 86400000);
            data2.setTime(data2.getTime() + 86400000);

            String format1 = simpleDateFormat.format(data1);
            String format2 = simpleDateFormat.format(data2);

            System.out.println(data1 + "," + data2);

            startTime = format1;
            endTime = format2;
        }
        QueryString queryStrings = new QueryString(
                (String) queryString.get("orderType"),
                startTime,
                endTime,
                (String) queryString.get("stringCondition"),
                (String) queryString.get("orderStatus"),
                (String) queryString.get("setmealName"));

        PageHelper.startPage(currentPage, pageSize);
        Page<OrderList> page = orderListDao.findPage(queryStrings);
        System.out.println(queryString);
        System.out.println("time!!!!" + startTime + "," + endTime);

        long total = page.getTotal();
        List<OrderList> result = page.getResult();
        System.out.println("total" + total);
        System.out.println("result" + result);
//        queryString.get("orderType");
//        queryString.get("stringCondition");
//        queryString.get("orderStatus");
//        queryString.get("orderTime");
//        queryString.get("setmealName");
        return new PageResult(total, result);
    }
}
